package v1

import (
	"gin-example/models"
	"gin-example/pkg/e"
	"gin-example/pkg/setting"
	"gin-example/pkg/util"
	"github.com/astaxie/beego/validation"
	"github.com/gin-gonic/gin"
	"github.com/unknwon/com"
	"net/http"
)

// GetTags 获取多个文章标签
func GetTags(c *gin.Context)  {
   name := c.Query("name")

   maps := make(map[string]interface{})
   data := make(map[string]interface{})

   if name != "" {
   	maps["name"] = name
   }

   var state int = -1
   if arg := c.Query("state"); arg != "" {
   	 state = com.StrTo(arg).MustInt()
   	 maps["state"] = state
   }

   code := e.SUCCESS

   data["lists"] = models.GetTags(util.GetPage(c),setting.PageSize,maps)
   data["total"] = models.GetTagTotal(maps)

   c.JSON(http.StatusOK, gin.H{
   	    "code": code,
   	    "msg": e.GetMsg(code),
   	    "data": data,
   })
}

// AddTag 新增文章标签
func AddTag(c *gin.Context) {
    name := c.Query("name")
    state := com.StrTo(c.DefaultQuery("state", "0")).MustInt()
    createdBy := c.Query("created_by")

    valid := validation.Validation{}
    valid.Required(name,"name").Message("名称不能为空")
    valid.MaxSize(name, 100,"name").Message("名称最长为100字符")
    valid.Required(createdBy,"created_by").Message("创建人不能为空")
    valid.MaxSize(createdBy,100,"created_by").Message("创建人最长为100字符")
    valid.Range(state, 0,1,"state").Message("状态只允许0或1")

    code := e.INVALID_PARAMS
    if ! valid.HasErrors() {
    	if ! models.ExistTagByName(name) {
    		code = e.SUCCESS
    		models.AddTag(name,state,createdBy)
		} else {
			code = e.ERROR_EXIST_TAG
		}
	}

	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg": e.GetMsg(code),
		"data":make(map[string]string),
	})
}

// EditTag 修改文章标签
func EditTag(c *gin.Context) {
  id := com.StrTo(c.Param("id")).MustInt()
  name := c.Query("name")
  modifiedBy := c.Query("modified_by")
  state := com.StrTo(c.Query("state")).MustInt()

  tag := models.Tag{
	  Name:       name,
	  ModifiedBy: modifiedBy,
	  State:      state,
  }

  code := e.INVALID_PARAMS
  if models.ExistTagByID(id) {
  	code = e.SUCCESS
  	models.EditTag(id, tag)
  } else {
    code = e.ERROR_NOT_EXIST_TAG
  }

  c.JSON(http.StatusOK, gin.H{
  	"code": code,
  	"msg": e.GetMsg(code),
  	"data": make(map[string]string),
  })

}

// DeleteTag 删除文章标签
func DeleteTag(c *gin.Context) {
  id := com.StrTo(c.Param("id")).MustInt()

  valid := validation.Validation{}
  valid.Min(id, 1, "id").Message("ID必须大于0")

  code := e.INVALID_PARAMS
  if ! valid.HasErrors() {
  	code = e.SUCCESS
  	if models.ExistTagByID(id){
  		models.DeleteTag(id)
	} else {
		code = e.ERROR_NOT_EXIST_TAG
	}
  }

  c.JSON(http.StatusOK, gin.H{
  	 "code": code,
  	 "msg": e.GetMsg(code),
  	 "data": make(map[string]string),
  })

}
